var BSTMG = BSTMG || {};
BSTMG.orderManager = {
	init: function(){
		var that = this;
		//分页功能
		that.showOrderList();
	},
	showOrderList: function(){
		BSTMG.common.hjhPages('/order/orderList/search',function(data){
			var orderList = data.orderList;
            var $tbody = $("#orderList tbody"),
                html = '';
            if(orderList.length > 0) {
				for(var i=0;i<orderList.length;i++){
					var orderInfo = orderList[i];
					var payTime;
					if(orderInfo.payTime){
						payTime = new Date(orderInfo.payTime);
						payTime = toDate(payTime, "yyyy-MM-dd hh:mm:ss");
					}else{
						payTime = '';
					}
					var userEmailOrTell= orderInfo.userEmail;
					if(userEmailOrTell == null || userEmailOrTell == ''){
						userEmailOrTell = orderInfo.userTel;
					}
					html += '<tr class="odd">';
					html += '<td>'+(i+1)+'</td>';
					html += '<td>'+BSTMG.tools.checkNull(orderInfo.orderNum)+'</td>';
					html += '<td>'+BSTMG.tools.checkNull(userEmailOrTell)+'</td>';
					html += '<td>'+BSTMG.tools.checkNull(orderInfo.belongCompanyName)+'</td>';
					html += '<td>'+BSTMG.tools.checkNull(orderInfo.companyDomainName)+'</td>';
					html += '<td>'+BSTMG.tools.checkNull(orderInfo.mealName)+'</td>';
					html += '<td>'+BSTMG.tools.checkNull(orderInfo.payMoney)+'</td>';
					html += '<td>'+BSTMG.tools.checkNull(payTime)+'</td>';
					html += '<td>'+setOrderState(orderInfo.orderState)+'</td>';
					html += '<td>'+setOrderType(orderInfo.isDredge)+'</td>';
					html += '<td><a class="btn btn-primary" href="/order/orderDetail/'+orderInfo.orderId+'">详情</a>';
					if(orderInfo.orderState == 0 && orderInfo.buyType==0 && orderInfo.newProId == null)
						html += '<button type="button" class="btn btn-danger" onclick="toRefund('+orderInfo.orderId+');">退单</button>';
					html += '</td></tr>';
					$tbody.html(html);
				}
            } else {
                var $tr = '<tr class="odd"><td colspan="7">暂无数据！</td></tr>';
                $tbody.html($tr);
                alert("查询失败！")
            }
		});
	}
}

$(function(){
	BSTMG.orderManager.init();
});

//退单操作
function toRefund (orderId){
	$.ajax({
		type : "POST",
		url : "/order/updateOrder",
		async : false,
		data:{
			orderId:orderId,
		},
		success:function(data){
			if(data) alert("退单成功！");
			else alert("退单失败！");
		}
	})
}

//检测非空
function checkNull(para){
	if(para == null){
		return "";
	}else{
		return para;
	}
}
 
//设置付款状态
function setOrderState(para){
	if (para == "0") {
		return "已付款";
	} else if (para == "1") {
		return "待付款";
	} else if(para == "2"){
		return "已取消";
	}
}

//设置开通状态
function setOrderType(para){
	if (para == "0") {
		return "未开通";
	} else if (para == "1") {
		return "已开通";
	}
}